var mongoose = require('mongoose');
require('../model/order.model');
var orderModel = mongoose.model('Order');

module.exports={
    addOrder:addOrder,
    confirmOrder:confirmOrder,
    findByOrderId:findByOrderId,
    findOrders:findOrders
}


function addOrder(orderInfo,callBack){
    orderModel.create(orderInfo,function(err,doc){
        callBack(err,doc);
    });
}


function confirmOrder(orderId,callBack){
    orderModel.update({_id:orderId},{status:3},function(err,doc){
        callBack(err,doc);
    });
}

function findByOrderId(orderId,callBack){
    orderModel.findOne({_id:orderId},function(err,doc){
        callBack(err,doc);
    }).populate('customer').populate('merchant');
}


function findOrders(customerId,callBack){
    orderModel.find({customer:customerId},function(err,doc){
        callBack(err,doc);
    }).sort({createTime:-1}).populate('merchant');
}




